import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 95. 不同的二叉搜索树 II
 * @author: WXY
 * @create: 2023-01-06 13:10
 * @Version 1.0
 **/
public class Num095_generateTrees {
    public static List<TreeNode> generateTrees(int n) {
        if (n == 0) return new ArrayList<>();
        return generateTrees(1, n);
    }

    private static List<TreeNode> generateTrees(int l, int r) {
        ArrayList<TreeNode> allList = new ArrayList<>();
        if (l > r) {
            allList.add(null);
            return allList;
        }
        for (int i = l; i <= r; i++) {
            List<TreeNode> leftTree = generateTrees(l, i - 1);
            List<TreeNode> rightTree = generateTrees(i + 1, r);
            for (TreeNode curLeft : leftTree) {
                for (TreeNode curRight : rightTree) {
                    TreeNode curNode = new TreeNode(i);
                    curNode.left = curLeft;
                    curNode.right = curRight;
                    allList.add(curNode);
                }
            }
        }
        return allList;
    }

    public static void main(String[] args) {
        List<TreeNode> treeNodes = generateTrees(3);
        System.out.println(treeNodes);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
